//
//  AXspellCell.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/11/12.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXspellCell.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "UIImageView+WebCache.h"

@interface AXspellCell ()

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation AXspellCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self creatView];
       
        
    }
    return self;
}

- (void)creatView {
    
    self.userImageView = [[UIImageView alloc]init];
    self.userImageView.cornerRadius = 25;
    /** 图片比例切 */
    self.userImageView .contentMode =UIViewContentModeScaleAspectFill;
    self.userImageView .clipsToBounds=YES;//
    self.userImageView.image = [UIImage imageNamed:@"xy.jpg"];
    [self.contentView addSubview:self.userImageView];
    
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.text = @"哆啦A梦";
    self.nameLabel.textColor = COLOR_DIV;
    self.nameLabel.font = FONT_FOURTEEN;
    [self.contentView addSubview:self.nameLabel];
    
    self.peopleLabel = [[UILabel alloc]init];
    self.peopleLabel.text = @"还差5人拼成";
    self.peopleLabel.textColor = COLOR_DIV;
    self.peopleLabel.font = FONT_FOURTEEN;
    [self.contentView addSubview:self.peopleLabel];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.textColor = COLOR_ORG;
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.timeLabel];
    
    
    self.spellBtn = [[UIButton alloc]init];
    [self.spellBtn setTitle:@"立即拼单" forState:UIControlStateNormal];
    self.spellBtn.backgroundColor = COLOR_ORG;
    [self.spellBtn addTarget:self action:@selector(shopAction:) forControlEvents:UIControlEventTouchUpInside];
    self.spellBtn.titleLabel.font = FONT_FOURTEEN;
    [self.spellBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.spellBtn.cornerRadius = 5;
    [self.contentView addSubview:self.spellBtn];
    
    
    
}


- (void)setlayout {
    
    self.userImageView.sd_layout
    .leftSpaceToView(self.contentView, 10)
    .centerYEqualToView(self.contentView)
    .widthIs(50)
    .heightIs(50);
    
    self.nameLabel.sd_layout
    .leftSpaceToView(self.userImageView, 10)
    .centerYEqualToView(self.userImageView)
    .widthIs(70)
    .heightIs(30);
    
    self.peopleLabel.sd_layout
    .leftSpaceToView(self.nameLabel, 10)
    .topSpaceToView(self.contentView, 10)
    .widthIs(100)
    .heightIs(30);
    
    self.timeLabel.sd_layout
    .leftSpaceToView(self.nameLabel, 10)
    .topSpaceToView(self.peopleLabel, 10)
    .widthIs(SCREEN_WIDTH-250)
    .heightIs(30);
    
    self.spellBtn.sd_layout
    .rightSpaceToView(self.contentView, 10)
    .centerYEqualToView(self.contentView)
    .widthIs(80)
    .heightIs(30);
    
    
    [self setupAutoHeightWithBottomView:self.spellBtn bottomMargin:10];
    
    
}


#pragma mark - shopAction

- (void)shopAction:(UIButton *)btn {
    
    if (self.tapCelllickedBlock) {
        
        self.tapCelllickedBlock(btn.tag);
    }
}


- (void)setModel:(AXspellModel *)model {
    _model = model;
  
   
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:_model.founderFacImage] placeholderImage:[UIImage imageNamed:@"placeHold"]];
    
    _nameLabel.text = _model.founderNickname;
    _peopleLabel.text = [NSString stringWithFormat:@"还差%ld人拼成",[_model.groupSuccessNeedNum integerValue] - [_model.currentJoinNum integerValue]];
    [self setlayout];
    [self aaaa];
    
}

- (NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}



- (NSTimeInterval)pleaseInsertStarTime:(NSString *)starTime andInsertEndTime:(NSString *)endTime{
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//根据自己的需求定义格式
    NSDate* startDate = [formater dateFromString:starTime];
    NSDate* endDate = [formater dateFromString:endTime];
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}

-(NSMutableArray *)getMMSSFromSS:(NSString *)totalTime{
    
    NSInteger seconds = [totalTime integerValue];
    
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    NSMutableArray *array = [NSMutableArray arrayWithObjects:str_hour, str_minute, str_second, nil];
    return array;
}


- (void)aaaa {
    
    __weak __typeof(self) weakSelf = self;
    NSTimeInterval time = [self pleaseInsertStarTime:[self getCurrentTimes] andInsertEndTime:_model.acEndDateTime];
    NSString *secondsCountDown =  [NSString stringWithFormat:@"%f", time];
//    NSArray *dateAry = [self getMMSSFromSS:[NSString stringWithFormat:@"%f", time]];
    if (_timer == nil) {
        __block NSInteger timeout = [secondsCountDown integerValue]; // 倒计时时间
        if (timeout!=0) {
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
            dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC,  0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout <= 0){ //  当倒计时结束时做需要的操作: 关闭 活动到期不能提交
                    dispatch_source_cancel(_timer);
                    _timer = nil;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        weakSelf.timeLabel.text = @"当前活动已结束";
                    });
                } else { // 倒计时重新计算 时/分/秒
//                    NSInteger days = (int)(timeout/(3600*24));
                    NSInteger hours = (int)((timeout)/3600);
                    NSInteger minute = (int)(timeout-hours*3600)/60;
                    NSInteger second = timeout - hours*3600 - minute*60;
                    NSString *strTime = [NSString stringWithFormat:@"剩余%02ld小时 : %02ld 分钟: %02ld秒", hours, minute, second];
//                    self.timeLabel.text = strTime;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSString *strTime = [NSString stringWithFormat:@"剩余%02ld小时 : %02ld分钟 : %02ld秒", hours, minute, second];
                        self.timeLabel.text = strTime;
                    });
                    timeout--; // 递减 倒计时-1(总时间以秒来计算)
                }
            });
            dispatch_resume(_timer);
        }
    }
}



@end
